#! /bin/bash
help_msg () {
  printf "Usage: bd [OPTION]... [PATTERN]\n"
  printf "Quickly go back to a specific parent directory in bash.\n\n"

  printf "\e[1mOPTIONS\e[0m\n"
  printf "  %-28s %s\n" "-s" "PATTERN can match part of directory name"
  printf "  %-28s %s\n" "-si" "PATTERN is Case Insensitve and can be partial"
  printf "  %-28s %s\n" "-?, --help" "Display this message"

  printf "\n\e[1mALTERNATE USAGE EXAMPLES\e[0m\n"
  printf "  %-28s %s\n" "\`bd -si som\`/script.sh" "Execute \"script.sh\" in matching path"

  return 0
}

usage_error () {
  cat << EOF
------------------------------------------------------------------
Name: bd
Version: 1.03

------------------------------------------------------------------
Description: Go back to a specified directory up in the hierarchy.

------------------------------------------------------------------
How to use:

Please refer https://github.com/vigneshwaranr/bd

EOF
  return 1
}

newpwd() {
  oldpwd=$1
  case "$2" in
    -s)
      pattern=$3
      NEWPWD=$(echo $oldpwd | sed 's|\(.*/'$pattern'[^/]*/\).*|\1|')
      ;;
    -si)
      pattern=$3
      NEWPWD=$(echo $oldpwd | perl -pe 's|(.*/'$pattern'[^/]*/).*|$1|i')
      ;;
    -?|--help)
      help_msg
      ;;
    *)
      pattern=$2
      NEWPWD=$(echo $oldpwd | sed 's|\(.*/'$pattern'/\).*|\1|')
  esac
}

if [ $# -eq 0 ]
then
  usage_error
elif [ "${@: -1}" = -v ]
then
  usage_error
else
  oldpwd=$(pwd)

  newpwd "$oldpwd" "$@"
  
  if [ "$NEWPWD" = "$oldpwd" ]
  then
    echo "No such occurrence."
  else
    echo $NEWPWD
    cd "$NEWPWD"
  fi
  unset NEWPWD
fi
